#!/bin/bash -e

source reporting.sh

REPORT=reports/report.txt
TEST_DESC="Verify all default passwords are updated and new Google provided passwords are set."

make_log_files() {
  touch /tmp/http_report.txt
  touch /tmp/https_report.txt
  touch /tmp/telnet_report.txt
  touch /tmp/ssh_report.txt
}

run_password_test_all_protocols () {
  java -jar security_passwords/build/libs/security_passwords-1.0-SNAPSHOT-all.jar $1 http 80 $2 nginx-site > /tmp/http_report.txt &

  java -jar security_passwords/build/libs/security_passwords-1.0-SNAPSHOT-all.jar $1 https 443 $2 nginx-site > /tmp/https_report.txt &

  java -jar security_passwords/build/libs/security_passwords-1.0-SNAPSHOT-all.jar $1 telnet 23 $2 nginx-site > /tmp/telnet_report.txt &

  java -jar security_passwords/build/libs/security_passwords-1.0-SNAPSHOT-all.jar $1 ssh 22 $2 nginx-site > /tmp/ssh_report.txt &

  wait
}

if [ -n "$TARGET_IP" ]; then
  run_password_test_all_protocols $TARGET_IP $TARGET_MAC

  make_log_files

  write_out_result $REPORT \
    "security.passwords.http" \
    "$TEST_DESC" \
    "$(cat /tmp/http_report.txt)" \
    "$(cat reports/http_result.txt)"

  write_out_result $REPORT \
    "security.passwords.https" \
    "$TEST_DESC" \
    "$(cat /tmp/https_report.txt)" \
    "$(cat reports/https_result.txt)"

  write_out_result $REPORT \
    "security.passwords.telnet" \
    "$TEST_DESC" \
    "$(cat /tmp/telnet_report.txt)" \
    "$(cat reports/telnet_result.txt)"

  write_out_result $REPORT \
    "security.passwords.ssh" \
    "$TEST_DESC" \
    "$(cat /tmp/ssh_report.txt)" \
    "$(cat reports/ssh_result.txt)"

  cp -r $REPORT /tmp/report.txt
else
  echo Problem with target IP, password test cannot continue. | tee /tmp/report.txt
fi
